package bscorp.appbase.activity;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.listener.DialogUIListener;

import java.util.ArrayList;
import java.util.List;

import bscorp.appbase.R;
import bscorp.appbase.adapter.DeviceAdapter;
import bscorp.appbase.bmo.DeviceBmo;
import bscorp.appbase.divider.EnvironmentMonitorItemDecoration;
import bscorp.appbase.model.Device;

public class DevicesListActivity extends AppCompatActivity {
    RecyclerView rv_list;
    List<Device> mDevices = new ArrayList<Device>();
    DeviceAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_devices_list);

        DialogUIUtils.init(this);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(DevicesListActivity.this, AddDeviceActivity.class));
            }
        });

        rv_list = (RecyclerView) findViewById(R.id.rv_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(DevicesListActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_list.setLayoutManager(layoutManager);
        mAdapter = new DeviceAdapter(R.layout.activity_device_list_item, mDevices);
        rv_list.setAdapter(mAdapter);
        rv_list.setItemAnimator(new DefaultItemAnimator());
        rv_list.addItemDecoration(new EnvironmentMonitorItemDecoration(DevicesListActivity.this, 10));
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(DevicesListActivity.this, AddDeviceActivity.class);
                intent.putExtra("device", mDevices.get(position));
                startActivity(intent);
            }
        });
        mAdapter.setOnItemChildLongClickListener(new BaseQuickAdapter.OnItemChildLongClickListener() {
            @Override
            public boolean onItemChildLongClick(BaseQuickAdapter adapter, View view, final int position) {
                DialogUIUtils.showMdAlert(DevicesListActivity.this, "标题", "确定删除该设备", new DialogUIListener() {
                    @Override
                    public void onPositive() {
                        new DeviceBmo().deleteDevice(mDevices.get(position));
                        setDatas();
                        DialogUIUtils.showToast("删除成功");
                    }

                    @Override
                    public void onNegative() {
                    }
                }).show();
                return true;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

      setDatas();
    }

    private void setDatas() {
        mDevices = new DeviceBmo().getDevices();
        mAdapter.setNewData(mDevices);
    }


    @Override
    public void onBackPressed() {
        finish();
    }
}
